<%--
  Created by IntelliJ IDEA.
  User: 29688
  Date: 2021/3/28
  Time: 13:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>书籍管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../layui-v2.5.7/layui/css/layui.css" media="all">
    <script src="../../../layui-v2.5.7/layui/layui.js" charset="utf-8"></script>
    <script src="../../../js/jquery.js"></script>
</head>
<body>

<table class="layui-hide" id="test" lay-filter="test">
</table>
<script type="text/html" id="toolbarDemo">
    <div>
        <button type="button" class="layui-btn layui-btn-sm">
            <i class="layui-icon">&#xe608;</i> 添加
        </button>
        <button type="button" class="layui-btn layui-btn-sm" lay-event="delSelect">
            <i class="layui-icon">&#xe640;</i> 删除
        </button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <div class="layui-inline">
            <input class="layui-input" id="bookNameInput" type="text" style="height: 32px;" placeholder="请输入要查询的书名">
        </div>
        <input class="layui-btn layui-btn-sm" type="button"  value="搜索"  id="searchByName" lay-event="searchByName">
    </div>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">类型变更</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="changeType">

</script>
<script>
    layui.use(['table','layer'], function () {
        var table = layui.table;
        table.render({
            elem: '#test'
            , url: '/book/selectAll'
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , title: '用户数据表'
            , cols: [[
                /*设置序号*/
                {type: 'numbers', fixed: 'left', title: "id"},
                {type: 'checkbox', fixed: 'left'}
                , {field: 'bid', title: 'ID', width: 80, fixed: 'left', hide: true}
                , {field: 'bname', title: '书名', width: 160, edit: 'text', sort: true}
                , {
                    field: 'price', title: '单价', width: 150, text: '未绑定', edit: 'text',sort: true
                }
                , {field: 'description', title: '描述', width: 200, edit: 'text'}
                , {field: 'count', title: '库存', width: 100,sort: true}
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150}
            ]]
            , page: true,
            skin: 'row',
            even: 'true',
            id: 'bookTable'
        });

        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'delSelect':
                    layer.confirm('真的删除所有选中行？', {icon: 3, title: '询问'}, function () {
                        var data = checkStatus.data;
                        if (data.length > 0) {
                            /*后端删除*/
                            var ids = []
                            for (let i = 0; i < data.length; i++) {
                                ids.push(data[i].bid)
                            }
                            console.log(ids);
                            /*前端删除*/
                            layer.confirm('确定删除选中' + data.length + '条数据？', {icon: 3, title: '提示信息'}, function (index) {
                                //layui中找到CheckBox所在的行，并遍历找到行的顺序
                                $("div.layui-table-body table tbody input[name='layTableCheckbox']:checked").each(function () { // 遍历选中的checkbox
                                    n = $(this).parents("tbody tr").index();  // 获取checkbox所在行的顺序
                                    //移除行
                                    $("div.layui-table-body table tbody ").find("tr:eq(" + n + ")").remove();
                                    //如果是全选移除，就将全选CheckBox还原为未选中状态
                                    $("div.layui-table-header table thead div.layui-unselect.layui-form-checkbox").removeClass("layui-form-checked");
                                });
                                deleteByIds(ids);
                                layer.close(index);
                            })
                        } else {
                            layer.msg("请选择需要删除的行");
                        }
                    });
                    break;
                case 'searchByName':
                    searchByName()
                    break;
            }

        });
        /*删除书籍的方法*/
        function deleteBooks(uid) {
            $.ajax({
                url: "/book/delete?bid=" + uid, function(data) {
                    console.log(data)
                }
            })
        }

        /*批量删除的方法*/
        function deleteByIds(ids) {
            $.ajax({
                type: "post",
                data: {"ids": ids},
                url: "/book/deleteByIds",
                dataType: 'json',
                traditional: true
            })
        }
        /*修改用户角色的方法*/
        function roleChange(uid,rid){
            $.ajax({
                data:{"uid":uid,"rid":rid},
                url:"/user/update"
            })
        }
        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                    deleteBooks(obj.data.bid)
                });
            } else if (obj.event === 'edit') {
                layer.open({
                    title:'书籍类型变更',
                    type:2,
                    content:'/user/bookType/type?bid='+obj.data.bid,
                    area: ['500px', '300px']
                })
            }
        });
        function searchByName(){
            console.log("搜索按钮被点击了")
            table.reload(
                'bookTable', {
                    where: { //设定异步数据接口的额外参数，任意设
                        url: '/book/selectAll',
                        bName: $('#bookNameInput').val()
                    }
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
        }
    });

</script>

</body>
</html>
